package com.guigu.qingcheng.orderservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.orderservice.entity.*;
import com.guigu.qingcheng.orderservice.mapper.TbOrderLogMapper;
import com.guigu.qingcheng.orderservice.mapper.TbOrderMapper;
import com.guigu.qingcheng.orderservice.mapper.TbReturnOrderMapper;
import com.guigu.qingcheng.orderservice.service.GoodsFeign;
import com.guigu.qingcheng.orderservice.service.TbOrderItemService;
import com.guigu.qingcheng.orderservice.service.TbReturnOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.qingcheng.pojo.goods.TbSku;
import com.guigu.qingcheng.pojo.order.TbOrder;
import com.guigu.qingcheng.pojo.order.TbOrderItem;
import com.guigu.qingcheng.pojo.order.TbOrderLog;
import com.guigu.qingcheng.pojo.order.TbReturnOrder;
import com.guigu.qingcheng.utils.IdWorker;
import com.guigu.qingcheng.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class TbReturnOrderServiceImpl extends ServiceImpl<TbReturnOrderMapper, TbReturnOrder> implements TbReturnOrderService {

    @Autowired
    private TbReturnOrderService tbReturnOrderService;
    @Resource
    private TbOrderLogMapper tbOrderLogMapper;
    @Resource
    private TbOrderMapper tbOrderMapper;
    @Resource
    private TbOrderItemService tbOrderItemService;
    @Resource
    private IdWorker idWorker;
    @Resource
    private GoodsFeign feign;


    @Override
    public R findPageth(Integer page, Integer size, Map<String, Object> searchMap) {
        Page<TbReturnOrder> tbReturnOrderPage = new Page<>(page,size);

        QueryWrapper<TbReturnOrder> wrapper = new QueryWrapper<>();
        if (searchMap != null) {
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id")))
                wrapper.eq("id",searchMap.get("id"));
            if (searchMap.get("status") != null && !"".equals(searchMap.get("status")))
                wrapper.like("status",searchMap.get("status"));
            if (searchMap.get("createTime") != null && !"".equals(searchMap.get("createTime")))
                wrapper.ge("apply_time",searchMap.get("createTime"));
            if (searchMap.get("createTime1") != null && !"".equals(searchMap.get("createTime1")))
                wrapper.le("apply_time",searchMap.get("createTime1"));
        }
        wrapper.eq("type",1);
        baseMapper.selectPage(tbReturnOrderPage,wrapper);
        return R.ok().data("total",tbReturnOrderPage.getTotal()).data("rows",tbReturnOrderPage.getRecords());
    }

    @Override
    public R updateOrder(TbReturnOrder tbReturnOrder) {
        tbReturnOrder.setStatus("1");
        tbReturnOrder.setAdminId(1);
        tbReturnOrder.setDisposeTime(new Date());
        boolean update = tbReturnOrderService.updateById(tbReturnOrder);
        if (update) {
            TbOrder tbOrder = tbOrderMapper.selectById(tbReturnOrder.getOrderId());
            if (tbOrder != null) {
                tbOrder.setPayStatus("2");
                tbOrderMapper.updateById(tbOrder);
            }
            List<TbOrderItem> byOrderId = tbOrderItemService.findByOrderId(tbOrder.getId());
            for (TbOrderItem tbOrderItem:byOrderId){
                tbOrderItem.setIsReturn("2");
                tbOrderItemService.updateById(tbOrderItem);
            }
            TbOrderLog log = new TbOrderLog();
            log.setOperater("system");
            log.setOperateTime(new Date());
            log.setOrderStatus(tbOrder.getOrderStatus());
            log.setPayStatus(tbOrder.getPayStatus());
            log.setConsignStatus(tbOrder.getConsignStatus());
            log.setRemarks(tbReturnOrder.getRemark());
            log.setOrderId(tbOrder.getId());
            tbOrderLogMapper.insert(log);
            return R.ok();
        }
        return R.error();
    }

    @Override
    public R findPagetk(Integer page, Integer size, Map<String, Object> searchMap) {
        Page<TbReturnOrder> tbReturnOrderPage = new Page<>(page,size);

        QueryWrapper<TbReturnOrder> wrapper = new QueryWrapper<>();
        if (searchMap != null) {
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id")))
                wrapper.eq("id",searchMap.get("id"));
            if (searchMap.get("status") != null && !"".equals(searchMap.get("status")))
                wrapper.like("status",searchMap.get("status"));
            if (searchMap.get("createTime") != null && !"".equals(searchMap.get("createTime")))
                wrapper.ge("apply_time",searchMap.get("createTime"));
            if (searchMap.get("createTime1") != null && !"".equals(searchMap.get("createTime1")))
                wrapper.le("apply_time",searchMap.get("createTime1"));
        }
        wrapper.eq("type",2);
        baseMapper.selectPage(tbReturnOrderPage,wrapper);
        return R.ok().data("total",tbReturnOrderPage.getTotal()).data("rows",tbReturnOrderPage.getRecords());
    }

    @Override
    public R deletes(String ids) {
        List<TbOrder> list = new ArrayList<>();
        String[] split = ids.split(",");
        for(String str:split){
            TbReturnOrder tbReturnOrder = baseMapper.selectById(str);
                TbOrder tbOrder = tbOrderMapper.selectById(tbReturnOrder.getOrderId());
                if (tbOrder != null) {
                    tbOrder.setIsDelete("1");
                    tbOrderMapper.updateById(tbOrder);
                }
                if (tbReturnOrder != null) {
                    TbOrderLog log = new TbOrderLog();
                    log.setOperater("system");
                    log.setOperateTime(new Date());
                    log.setOrderStatus(tbOrder.getOrderStatus());
                    log.setPayStatus(tbOrder.getPayStatus());
                    log.setConsignStatus(tbOrder.getConsignStatus());
                    log.setRemarks("删除订单");
                    log.setOrderId(tbOrder.getId());
                    tbOrderLogMapper.insert(log);
                    baseMapper.deleteById(Long.parseLong(str));
                }
            }
            return R.ok();
        }

        @Override
    public R bhupdateOrder(TbReturnOrder tbReturnOrder) {
        tbReturnOrder.setStatus("2");
        tbReturnOrder.setAdminId(1);
        tbReturnOrder.setDisposeTime(new Date());
        TbOrder tbOrder = tbOrderMapper.selectById(tbReturnOrder.getOrderId());
        List<TbOrderItem> byOrderId = tbOrderItemService.findByOrderId(tbOrder.getId());
        for (TbOrderItem tbOrderItem:byOrderId){
            tbOrderItem.setIsReturn("0");
            tbOrderItemService.updateById(tbOrderItem);
        }
        TbOrderLog log = new TbOrderLog();
        log.setOperater("system");
        log.setOperateTime(new Date());
        log.setOrderStatus(tbOrder.getOrderStatus());
        log.setPayStatus(tbOrder.getPayStatus());
        log.setConsignStatus(tbOrder.getConsignStatus());
        log.setRemarks(tbReturnOrder.getRemark());
        log.setOrderId(tbReturnOrder.getOrderId().toString());
        tbOrderLogMapper.insert(log);
        boolean update = tbReturnOrderService.updateById(tbReturnOrder);
        if (update)
            return R.ok();
        return R.error();
    }

    @Override
    public R addOrder(TbOrder tbOrder, String radio, String result, String remarks, String yy) {
        TbReturnOrder returnOrder = new TbReturnOrder();
        returnOrder.setOrderId(Long.parseLong(tbOrder.getId()));
        returnOrder.setApplyTime(new Date());
        returnOrder.setUserAccount(tbOrder.getUsername());
        returnOrder.setLinkman(tbOrder.getReceiverContact());
        returnOrder.setLinkmanMobile(tbOrder.getReceiverMobile());
        returnOrder.setType("2");
        if (Integer.parseInt(radio) == 0) {
            returnOrder.setIsReturnFreight("0");
            returnOrder.setReturnMoney(Integer.parseInt(result)-tbOrder.getPreMoney());
        } else if (Integer.parseInt(radio) == 1) {
            returnOrder.setIsReturnFreight("1");
            returnOrder.setReturnMoney(Integer.parseInt(result)+tbOrder.getPostFee()-tbOrder.getPreMoney());
        }
        returnOrder.setStatus("0");
        returnOrder.setDescription(yy);
        returnOrder.setAdminId(1);
        returnOrder.setId(idWorker.nextId());
        baseMapper.insert(returnOrder);
        TbOrderLog log = new TbOrderLog();
        tbOrder.setOrderStatus("4");
        tbOrderMapper.updateById(tbOrder);
        log.setOperater("system");
        log.setOperateTime(new Date());
        log.setOrderStatus(tbOrder.getOrderStatus());
        log.setPayStatus(tbOrder.getPayStatus());
        log.setConsignStatus(tbOrder.getConsignStatus());
        log.setRemarks(remarks);
        log.setOrderId(tbOrder.getId());
        tbOrderLogMapper.insert(log);
        return R.ok();
    }

    @Override
    public R updateOrderId(List<MyOrder> myOrders) {
        for (MyOrder myOrder:myOrders) {
            TbOrderItem tbOrderItem = tbOrderItemService.getById(myOrder.getId());
            TbOrder order = tbOrderMapper.selectById(myOrder.getOrder_id());
            TbSku sku = feign.findBySkuByOrder(myOrder.getSku_id());
            if (myOrder.getNum() > tbOrderItem.getNum()) {
                order.setTotalNum(order.getTotalNum()+(myOrder.getNum()- tbOrderItem.getNum()));
                order.setTotalMoney(order.getTotalMoney()+((myOrder.getNum()- tbOrderItem.getNum())*tbOrderItem.getPrice()));
                sku.setNum(sku.getNum()-(myOrder.getNum()- tbOrderItem.getNum()));
                tbOrderItem.setMoney(((myOrder.getNum()- tbOrderItem.getNum())*tbOrderItem.getPrice())+tbOrderItem.getMoney());
                tbOrderItem.setNum(((myOrder.getNum()- tbOrderItem.getNum())+tbOrderItem.getNum()));
                feign.updateSkuByOrder(sku);
                tbOrderItemService.updateById(tbOrderItem);
                tbOrderMapper.updateById(order);
            } else if (myOrder.getNum() < tbOrderItem.getNum()) {
                order.setTotalNum(order.getTotalNum()-(tbOrderItem.getNum()-myOrder.getNum()));
                order.setTotalMoney(order.getTotalMoney()-((tbOrderItem.getNum() - myOrder.getNum())*tbOrderItem.getPrice()));
                sku.setNum(sku.getNum()+( tbOrderItem.getNum() - myOrder.getNum()));
                tbOrderItem.setMoney(tbOrderItem.getMoney()-((tbOrderItem.getNum()-myOrder.getNum())*tbOrderItem.getPrice()));
                tbOrderItem.setNum(tbOrderItem.getNum()-((tbOrderItem.getNum()-myOrder.getNum())));
                feign.updateSkuByOrder(sku);
                tbOrderItemService.updateById(tbOrderItem);
                tbOrderMapper.updateById(order);
            }
            TbOrderLog log = new TbOrderLog();
            log.setOperater("system");
            log.setOperateTime(new Date());
            log.setOrderStatus(order.getOrderStatus());
            log.setPayStatus(order.getPayStatus());
            log.setConsignStatus(order.getConsignStatus());
            log.setRemarks("修改商品信息");
            log.setOrderId(order.getId());
            tbOrderLogMapper.insert(log);
        }
        return R.ok();
    }

}
